<?php

class CarsController extends Auto_Controller_Simple
{
    public function init()
    {
        $this->_model = new Car();
        $this->_page = 'cars';
        $this->_title = 'Автомобили';
    }

    public function indexAction()
    {
        $make = new Make();
        $this->view->makes = array('0' => '-- Марка --') + $make->getOptions();

        $this->view->years = array('0' => '-- Година от --') + $this->_getYears();
        $this->view->years = array('0' => '-- Година до --') + $this->_getYears();

        $filters = array();

        $make_id = $this->_getParam('make_id');
        if (!empty($make_id)) {
            $this->view->make_id = $make_id;
            $filters['make_id'] = $make_id;
        }

        $year_from = $this->_getParam('year_from');
        if (!empty($year_from)) {
            $this->view->year_from = $year_from;
            $filters['year_from'] = $year_from;
        }

        $year_to = $this->_getParam('year_to');
        if (!empty($year_to)) {
            $this->view->year_to = $year_to;
            $filters['year_to'] = $year_to;
        }

        $keyword = trim($this->_getParam('keyword'));
        if (!empty($keyword)) {
            $this->view->keyword = $keyword;
            $filters['keyword'] = $keyword;
        }

        $this->view->items = $this->_model->getList($filters);
    }

    public function addAction()
    {
        $form = $this->_getCarForm();

        if ($this->getRequest()->isPost() == false || $form->isValid($_POST) == false) {
            $this->view->carForm = $form;
            return;
        }

        $values = $form->getValues();
        $this->_model->save($values);
        $this->_helper->redirector('index', 'cars');
    }

    public function editAction()
    {
        $carId = $this->_getParam('id');
        $form = $this->_getCarForm($carId);

        if ($this->getRequest()->isPost() == false) {
            $t = $this->_model->get($carId);
            $car = $t['car'];
            $data = array(
                'make_id'       => $car->make_id,
                'model'         => $car->model,
                'price'         => $car->price,
                'produced'      => $car->produced,
                'kms'           => $car->kms,
                'fuel_id'       => $car->fuel_id,
                'trans_id'      => $car->trans_id,
                'style_id'      => $car->style_id,
                'color_id'      => $car->color_id,
                'info'          => $car->info,
                'features'      => $t['features'],
            );

            $this->view->carForm = $form->populate($data);
            return;
        } else if ($form->isValid($_POST) == false) {
            $this->view->carForm = $form;
            return;
        }

        $values = $form->getValues();
        $this->_model->save($values, $carId);
        $this->_helper->redirector('index', 'cars');
    }

    /**
     * Create car form
     *
     * @return Zend_Form
     */
    private function _getCarForm($id = null)
    {
        $form = new Zend_Form();
        $form->setMethod('post')->setAttrib('id', 'car_form');
        if (isset($id)) {
            $form->setAction('/cars/edit/id/' . $id);
        } else {
            $form->setAction('/cars/add');
        }
        $form->setElementFilters(array('StringTrim'));

        $makeId = new Zend_Form_Element_Select('make_id');
        $makeId->setLabel('Марка');
        $makeId->setRequired();
        $make = new Make();
        $makeId->addMultiOptions($make->getOptions());
        $form->addElement($makeId);

        $name = new Zend_Form_Element_Text('model');
        $name->setLabel('Модел');
        $name->setRequired();
        $form->addElement($name);

        $price = new Zend_Form_Element_Text('price');
        $price->setLabel('Цена (лв)');
        //$price->addFilter('Digits');
        $form->addElement($price);

        $produced = new Zend_Form_Element_Select('produced');
        $produced->setLabel('Година');
        $produced->addMultiOptions($this->_getYears());
        $form->addElement($produced);

        $kms = new Zend_Form_Element_Text('kms');
        $kms->setLabel('Пробег (km)');
        //$kms->addFilter('Digits');
        $form->addElement($kms);

        $fuelId = new Zend_Form_Element_Select('fuel_id');
        $fuelId->setLabel('Вид двигател');
        $fuel = new Fuel();
        $fuelId->addMultiOptions($fuel->getOptions());
        $form->addElement($fuelId);

        $transId = new Zend_Form_Element_Select('trans_id');
        $transId->setLabel('Скоростна кутия');
        $trans = new Transmission();
        $transId->addMultiOptions($trans->getOptions());
        $form->addElement($transId);

        $styleId = new Zend_Form_Element_Select('style_id');
        $styleId->setLabel('Категория');
        $style = new Style();
        $styleId->addMultiOptions($style->getOptions());
        $form->addElement($styleId);

        $colorId = new Zend_Form_Element_Select('color_id');
        $colorId->setLabel('Цвят');
        $color = new Color();
        $colorId->addMultiOptions($color->getOptions());
        $form->addElement($colorId);

        $info = new Zend_Form_Element_Textarea('info');
        $info->setLabel('Информация');
        $info->setAttrib('cols', 50)->setAttrib('rows', 8);
        $form->addElement($info);

        $features = new Zend_Form_Element_MultiCheckbox('features');
        $features->setLabel('Особености');
        $feature = new Feature();
        $features->addMultiOptions($feature->getOptions());
        $form->addElement($features);

        $submit = new Zend_Form_Element_Submit('save');
        $submit->setLabel('Запис');
        $form->addElement($submit);

        $form->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'table')),
            'Form'
        ));

        $form->setElementDecorators(array(
            'ViewHelper',
            'Errors',
            array(array('data' => 'HtmlTag'), array('tag' => 'td')),
            array('Label', array('tag' => 'td')),
            array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
        ));

        $submit->setDecorators(array(
            'ViewHelper',
            array(array('data' => 'HtmlTag'), array('tag' => 'td')),
            array(array('label' => 'HtmlTag'), array('tag' => 'td', 'placement' => 'prepend')),
            array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
        ));

        return $form;
    }

    private function _getYears()
    {
        $currentYear = intval(date('Y'));

        $out = array();
        for ($i = 1970; $i <= $currentYear; $i++) {
            $out[$i] = $i;
        }

        return $out;
    }
}
